home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / AMOSList / AMOSLIST / text0110.txt < prev    next >
Encoding:
Text File  |  1998-04-01  |  1.9 KB  |  68 lines

  1. > --text.txt look like this--
  2. > 5
  3. > 8
  4. > --------------------
  5. > R$="text.txt"
  6. > Open In 1,R$ : A=Lof(1) : Close 1
  7. > Reserve As Work 16,A : Bload R$,16 : ABC=Start(16)
  8. > C1=Peek(ABC) : Inc ABC : C2=Peek(ABC)
  9. > Print C1         : rem gives the ascii value
  10. > Print Chr$(C1)       : show numeral 5
  11. > ---------------------
  12. > Ok how will do to make amos to understand that i want to treat this
  13. > as a number instead of an ascii value?
  14. > Is there anyway to convert that 5 (53) so 
  15. > i can use it like this:
  16. > TAL=C1+C2 
  17. > Print TAL      : want it to be 13
  18.  
  19.  
  20.    Sure, there is always a way... :-)
  21.  
  22. -----<CODE BEGINS HERE>-----
  23. Global GRABVALUE
  24.  
  25. Procedure GRABDIGITS[MEMLOC, NUMDIGITS]
  26.    GRABVALUE=0 
  27.    While NUMDIGITS
  28.       DIGIT=Peek(MEMLOC) : Inc MEMLOC
  29.       ' IF this is really a digit, add it's value to the total...
  30.       If (DIGIT>47 And DIGIT<58)
  31.          Add GRABVALUE, DIGIT-48         
  32.          Dec NUMDIGITS
  33.       End If
  34.    Wend
  35. End Proc
  36.  
  37. ' Main Code here... 
  38. R$="text.txt"
  39. Open In 1,R$ : TFSIZE=Lof(1) : Close 1
  40. Reserve As Work 16, TFSIZE : ABC=Start(16)
  41. Bload R$, ABC 
  42. GRABDIGITS[ABC, 2] : TAL=GRABVALUE
  43. Print TAL : Rem ...should be 13
  44. End
  45. -----<CODE ENDS HERE>-----
  46.  
  47.    That should do the trick... It won't matter if the digits are seperated
  48.    by commas, spaces, carriage returns, etc. as GRABDIGITS will
  49.    simply skip over non-digits until it has gathered the quantity you
  50.    specified.
  51.  
  52.    Really, this would be better if implemented with a parameter
  53.    returned from the function, but I really don't like the Param
  54.    style AMOS uses, it makes more sense to me to use a GLOBAL
  55.    variable (GRABVALUE in this case)...
  56.  
  57.    Suggestion for AMOS Version 3.0 = Support for "REAL" function
  58.    style procedures, as in:
  59.  
  60.                        Print GRABDIGITS(ABC, 2)
  61.                                    or at least
  62.                        TAL=GRABDIGITS(ABC, 2)
  63.  
  64.  
  65.                 Garfield Benjamin    e-mail:gbenjam@sosbbs.com
  66.         Website( http://www.sosbbs.com/~gbenjam ): 50% Complete
  67.  
  68.